//
//  LGLungFollowEditCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/5.
//  肺癌-随访计划-编辑

#import "LGLungFollowEditTaskCell2.h"
#import "LGFollowKeywordView.h"
#import "LGInputAlertView.h"
@interface LGLungFollowEditTaskCell2()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *myTableView;
/**   */
@property (nonatomic , strong)UIButton *addButton;
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)UIView *maskView;
/**  随访项目 */
@property (nonatomic , strong)NSArray *examArray;
/**  评估量表 */
@property (nonatomic , strong)NSArray *evaArray;
/**  宣教项目 */
@property (nonatomic , strong)NSArray *preachArray;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGLungFollowEditTaskCell2

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.type = LGPositionTypeMiddle;
        [self.contentView addSubview:self.myTableView];
        [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(8));
            make.left.equalTo(self.contentView).offset(viewPix(28));
            make.right.equalTo(self.contentView).offset(-viewPix(28));
            make.bottom.equalTo(self.contentView).offset(-viewPix(8));
            make.height.equalTo(@(viewPix(460)));
        }];
    }
    return  self;
}

-(void)setCandelete:(BOOL)candelete{
    _candelete = candelete;
    self.maskView.hidden = candelete;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.examArray = dataDic[@"exams"];
    self.evaArray = dataDic[@"evas"];
    self.preachArray = dataDic[@"preachs"];
    [self.myTableView reloadData];
}

//删除
-(void)deleteButtonAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(deleteTaskAtIndexPath:)]) {
        [self.delegate deleteTaskAtIndexPath:self.indexPath];
    }
}

//新增
-(void)addButtonAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addTaskAtIndexPath:)]) {
        [self.delegate addTaskAtIndexPath:self.indexPath];
    }
}

//添加关键字
-(void)addKeywordAction{
    LGInputAlertView *nameView = [[LGInputAlertView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    nameView.placeHolder = @"请输入关键字";
    nameView.title = @"添加关键字";
    __weak typeof(self) weakSelf = self;
    nameView.groupNameFinished = ^(NSString * _Nonnull content) {
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:weakSelf.dataDic];
        NSMutableArray *keywordsArray = [NSMutableArray arrayWithArray:tempDic[@"keywords"]];
        [keywordsArray addObject:content];
        [tempDic setValue:keywordsArray forKey:@"keywords"];
        if (self.delegate && [self.delegate respondsToSelector:@selector(editKeywords:indexPath:)]) {
            [self.delegate editKeywords:tempDic indexPath:weakSelf.indexPath];
        }
    };
    [[ObjectTool mainWindow] addSubview:nameView];
}

//删除关键字
-(void)deleteKeywordItem:(NSInteger)index{
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataDic];
    NSMutableArray *keywordsArray = [NSMutableArray arrayWithArray:tempDic[@"keywords"]];
    [keywordsArray removeObjectAtIndex:index];
    [tempDic setValue:keywordsArray forKey:@"keywords"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(editKeywords:indexPath:)]) {
        [self.delegate editKeywords:tempDic indexPath:self.indexPath];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.examArray.count>0?self.examArray.count:1;
    }else if (section == 1){
        return self.evaArray.count>0?self.evaArray.count:1;
    }else if (section == 2){
        return self.preachArray.count>0?self.preachArray.count:1;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGEditTaskItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0  && indexPath.row<self.evaArray.count) {
        //随访项目
        cell.dataDic = self.examArray[indexPath.row];
        
    }else if (indexPath.section == 1  && indexPath.row<self.evaArray.count){
        //评估量表
        cell.dataDic = self.evaArray[indexPath.row];
        
    }else if (indexPath.section == 2 && indexPath.row<self.preachArray.count){
        //宣教项目
        cell.dataDic = self.preachArray[indexPath.row];
    }else{
        cell.dataDic = @{};
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *titleArray = @[@"随访项目",@"评估量表",@"宣教项目"];
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(56), viewPix(30))];
    UILabel *titleLabel = [UILabel lableWithText:titleArray[section] colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(10), viewPix(0), viewPix(150), viewPix(22));
    [baseView addSubview:titleLabel];
    return baseView;
}

//随访项目、评估量表、宣教项目
//-(void)tapGestureAction:(UITapGestureRecognizer *)sender{
//    CGPoint point = [sender locationInView:self.backView];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(editTaskAction:title:dataDic:key:indexPath:)]) {
//        if (CGRectContainsPoint(self.projectView.frame, point)) {
//            //选择随访项目
//            [self.delegate editTaskAction:@"/common/dict/examsDict" title:@"添加随访项目" dataDic:self.dataDic[@"codes"] key:@"codes" indexPath:self.indexPath];
//            
//        }else if (CGRectContainsPoint(self.evaluateView.frame, point)){
//            //选择评估量表
//            [self.delegate editTaskAction:@"/doctor/select/scalesSelect" title:@"添加评估量表" dataDic:self.dataDic[@"evacodes"] key:@"evacodes" indexPath:self.indexPath];
//            
//        }else if (CGRectContainsPoint(self.publicView.frame, point)){
//            //选择宣教项目
//            [self.delegate editTaskAction:@"/doctor/select/preachSelect" title:@"添加宣教项目" dataDic:self.dataDic[@"preachcodes"] key:@"preachcodes" indexPath:self.indexPath];
//        }
//    }
//}

-(UITableView *)myTableView{
    if(!_myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(viewPix(28), viewPix(8), Screen_W-viewPix(56), viewPix(460)) style:UITableViewStyleGrouped];
        [_myTableView registerClass:[LGEditTaskItemCell class] forCellReuseIdentifier:@"cell"];
        _myTableView.backgroundColor = [UIColor colorWithString:@"#FAFAFA"];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.scrollEnabled = NO;
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.cornerRidus = 10;
        _myTableView.tableFooterView = [self tableFootView];
        _myTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(56), viewPix(10))];
    }
    return _myTableView;
}

-(UIView *)tableFootView{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(56), viewPix(viewPix(40)))];
    [footView addSubview:self.deleteBtn];
    [footView addSubview:self.addButton];
    [footView addSubview:self.maskView];
    return footView;
}


-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFont(17) normalColor:@"#FF3B30" selectColor:@"#FF3B30"];
        _deleteBtn.frame = CGRectMake(viewPix(10), viewPix(4), (Screen_W-viewPix(92))/2.0, viewPix(32));
        [_deleteBtn addBorder:@"#FF3B30" lineWidth:1 cornerRidus:viewPix(16)];
        [_deleteBtn addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"新增" titleFont:LGFont(17) normalColor:@"#5E8EF5" selectColor:@"#5E8EF5"];
        _addButton.frame = CGRectMake(kMaxX(self.deleteBtn.frame)+viewPix(16), viewPix(4), (Screen_W-viewPix(92))/2.0, viewPix(32));
        [_addButton addBorder:@"#5E8EF5" lineWidth:1 cornerRidus:viewPix(16)];
        [_addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(UIView *)maskView{
    if(!_maskView){
        _maskView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(10), viewPix(4), (Screen_W-viewPix(92))/2.0, viewPix(32))];
        _maskView.backgroundColor = [UIColor colorWithString:@"#FFFFFF" alpha:0.7];
        _maskView.hidden = self.candelete;
    }
    return _maskView;
}
@end


#pragma mark
#pragma mark ====> LGEditTaskItemCell
@interface LGEditTaskItemCell()<LGFollowKeywordDelegate>
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIButton *maskBtn;//拦截cell的点击事件
/**   */
@property (nonatomic , strong)UILabel *keywordTitle;
/**   */
@property (nonatomic , strong)UIView *keywordView;
/**   */
@property (nonatomic , strong)UIButton *keywordBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGEditTaskItemCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *nameStr = @"";
    NSArray *nameArray = @[];
    NSArray *keywordsArray = @[];
    if (dataDic.allKeys.count>0) {
        nameArray = dataDic[@"codes"][@"names"];
        keywordsArray = dataDic[@"keywords"];
    }
    for (NSString *content in nameArray) {
        nameStr = (nameStr.length>0)?[NSString stringWithFormat:@"%@、%@",nameStr,content]:content;
    }
    self.contentLabel.text = (nameStr.length>0)?nameStr:@"请选择";
    NSString *contentColor = (nameStr.length>0)?@"#282828":@"#909399";
    self.contentLabel.textColor = [UIColor colorWithString:contentColor];
    for (LGFollowKeywordView *itemView in self.keywordView.subviews) {
        [itemView removeFromSuperview];
    }
    self.itemArray = [NSMutableArray array];
    CGFloat itemTop = 0;
    CGFloat itemLeft = 0;
    CGFloat itemBottom = 0;
    CGFloat maxWidth = Screen_W-viewPix(56);
    for (NSInteger i=0 ; i<keywordsArray.count; i++) {
        NSString *content = LGNSString(keywordsArray[i]);
        CGFloat contentW = [content textWidthWithFont:LGFont(15) maxHeight:MAXFLOAT]+viewPix(40);
        contentW = (contentW>maxWidth)?maxWidth:contentW;
        CGFloat contentH = [content textHeightWithFont:LGFont(15) maxWidth:maxWidth-viewPix(40)];
        itemTop = (itemLeft+contentW)>maxWidth?(itemTop+contentH+viewPix(28)):itemTop;
        itemLeft = (itemLeft+contentW)>maxWidth?0:itemLeft;
        LGFollowKeywordView *itemView = [[LGFollowKeywordView alloc]initWithFrame:CGRectMake(itemLeft, itemTop, contentW, contentH+viewPix(15))];
        itemView.content = content;
        [self.keywordView addSubview:itemView];
        itemLeft += contentW+viewPix(8);
        itemView.delegate = self;
        itemView.tag = 1000+i;
        [self.itemArray addObject:itemView];
        itemBottom = kMaxY(itemView.frame);
    }
    [self.keywordView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(itemBottom));
    }];
    

    
}

//添加关键字
-(void)addKeywordAction{
    LGInputAlertView *nameView = [[LGInputAlertView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    nameView.placeHolder = @"请输入关键字";
    nameView.title = @"添加关键字";
    __weak typeof(self) weakSelf = self;
    nameView.groupNameFinished = ^(NSString * _Nonnull content) {
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:weakSelf.dataDic];
        NSMutableArray *keywordsArray = [NSMutableArray arrayWithArray:tempDic[@"keywords"]];
        [keywordsArray addObject:content];
        [tempDic setValue:keywordsArray forKey:@"keywords"];
//        if (self.delegate && [self.delegate respondsToSelector:@selector(editKeywords:indexPath:)]) {
//            [self.delegate editKeywords:tempDic indexPath:weakSelf.indexPath];
//        }
    };
    [[ObjectTool mainWindow] addSubview:nameView];
}

//删除关键字
-(void)deleteKeywordItem:(NSInteger)index{
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataDic];
    NSMutableArray *keywordsArray = [NSMutableArray arrayWithArray:tempDic[@"keywords"]];
    [keywordsArray removeObjectAtIndex:index];
    [tempDic setValue:keywordsArray forKey:@"keywords"];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(editKeywords:indexPath:)]) {
//        [self.delegate editKeywords:tempDic indexPath:self.indexPath];
//    }
}

//随访项目、评估量表、宣教项目
//-(void)tapGestureAction:(UITapGestureRecognizer *)sender{
//    CGPoint point = [sender locationInView:self.backView];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(editTaskAction:title:dataDic:key:indexPath:)]) {
//        if (CGRectContainsPoint(self.projectView.frame, point)) {
//            //选择随访项目
//            [self.delegate editTaskAction:@"/common/dict/examsDict" title:@"添加随访项目" dataDic:self.dataDic[@"codes"] key:@"codes" indexPath:self.indexPath];
//            
//        }else if (CGRectContainsPoint(self.evaluateView.frame, point)){
//            //选择评估量表
//            [self.delegate editTaskAction:@"/doctor/select/scalesSelect" title:@"添加评估量表" dataDic:self.dataDic[@"evacodes"] key:@"evacodes" indexPath:self.indexPath];
//            
//        }else if (CGRectContainsPoint(self.publicView.frame, point)){
//            //选择宣教项目
//            [self.delegate editTaskAction:@"/doctor/select/preachSelect" title:@"添加宣教项目" dataDic:self.dataDic[@"preachcodes"] key:@"preachcodes" indexPath:self.indexPath];
//        }
//    }
//}


-(void)doNothing{
    //为了拦截cell的点击事件
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#FAFAFA"];
    self.contentView.backgroundColor = [UIColor colorWithString:@"#FAFAFA"];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.contentLabel];
    [self.contentView addSubview:self.addButton];
    [self.contentView addSubview:self.deleteBtn];
    [self.contentView addSubview:self.maskBtn];
    [self.contentView addSubview:self.keywordTitle];
    [self.contentView addSubview:self.keywordBtn];
    [self.contentView addSubview:self.keywordView];
    
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(2));
        make.left.equalTo(self.contentView).offset(viewPix(10));
        make.right.equalTo(self.contentView).offset(-viewPix(70));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(10));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(28));
        make.bottom.equalTo(self.baseView).offset(-viewPix(10));
    }];
    [self.maskBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView.mas_bottom);
        make.left.bottom.right.equalTo(self.contentView);
    }];
    [self.keywordTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView.mas_bottom).offset(viewPix(16));
        make.left.equalTo(self.baseView);
    }];
    [self.keywordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(10));
        make.centerY.equalTo(self.keywordTitle);
        make.width.height.equalTo(@(viewPix(35)));
    }];
    [self.keywordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.keywordTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.baseView);
        make.bottom.equalTo(self.contentView).offset(-viewPix(12));
        make.height.equalTo(@(0));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]init];
        _baseView.backgroundColor = [UIColor whiteColor];
        [_baseView addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        [_baseView addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_baseView).offset(-viewPix(12));
            make.centerY.equalTo(_baseView);
            make.width.height.equalTo(@(16));
        }];
    }
    return _baseView;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}


-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithImage:@"随访计划-新增" selectImage:@"随访计划-新增"];
        [_addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(UILabel *)keywordTitle{
    if(!_keywordTitle){
        _keywordTitle = [UILabel lableWithText:@"关键字" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _keywordTitle;
}

-(UIView *)keywordView{
    if(!_keywordView){
        _keywordView = [[UIView alloc]init];
    }
    return _keywordView;
}

-(UIButton *)keywordBtn{
    if(!_keywordBtn){
        _keywordBtn = [UIButton buttonWithImage:@"分组管理-编辑" selectImage:@"分组管理-编辑"];
        [_keywordBtn addTarget:self action:@selector(addKeywordAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _keywordBtn;
}

-(UIButton *)maskBtn{
    if(!_maskBtn){
        _maskBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_maskBtn addTarget:self action:@selector(doNothing) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskBtn;
}

@end
